package cn.wolfcode.rbac.web.controller.system_manage;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.system_manage.IDepartmentService;
import cn.wolfcode.rbac.util.PageResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @version 1.0
 * @Author:Cao Zhijianu0:05:37
 */
@Controller
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    /**
     * 部门列表的显示
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @param model :
     * @return : java.lang.String
     */
    @RequestMapping("/department/list")
    @RequiredPermission({"部门列表","department:list"})
    public String list(Model model, QueryObject queryObject){

        // 查询出数据
        PageResult result = iDepartmentService.query(queryObject);
//        System.out.println(departments);
        // 回显数据
        model.addAttribute("result",result);

        return "/department/list";
    }

    /**
     * 删除部门
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @return : java.lang.String
     */
    @RequestMapping("/department/delete")
    @RequiredPermission({"部门删除","department:delete"})
    public String delete(Long id){
        iDepartmentService.delete(id);

        // 重定向列表界面
        return "redirect:/department/list";
    }

    /**
     * 新增和编辑部门
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:14
     * @param id : 部门id
     * @return : java.lang.String
     */
    @RequestMapping("/department/input")
    public String input(Model model, Long id){

        if(id != null){
            // 如果id不为空，则为编辑功能，编辑功能要回显数据
            // 根据部门id查找部门信息
            Department department = iDepartmentService.get(id);
            model.addAttribute("entity",department);
        }
        // 返回到编辑界面
        return "/department/input";
    }

    /**
     * 新增和编辑部门的保存效果
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:27
     * @param department :
     * @return : java.lang.String
     */
    @RequestMapping("/department/saveOrUpdate")
    @RequiredPermission({"部门新增或编辑","department:saveOrUpdate"})
    public String saveOrUpdate(Department department){

        iDepartmentService.saveOrUpdate(department);

        return "redirect:/department/list";
    }
}
